<?php
/**
 * 角色控制器
 */
namespace Admin\Controller;
use Think\Controller;
class RoleController extends InitController {
	
	// 列表
	public function lists(){
		$roleModel = M('Role');
		$where = array('status'=>array('NEQ',-1));
		$total = $roleModel->where($where)->count();
	
		$Page = $this->setPageHander($total);
		$show = $Page?$Page->show():'';
	
		$list = $roleModel->where($where)
			->order('id asc')
			->limit($Page->firstRow.','.$Page->listRows)
			->select();
	
	
		$topmenu = $this->topmenu;
		$sidebarmenu = $this->sidebarmenu[I('get.k')];
		
		$this->assign('result',$result);
		$this->assign('topmenu',$topmenu);
		$this->assign('sidebarmenu',$sidebarmenu);
		$this->assign('pager',$show);
		$this->assign('list',$list);
		$this->display('Role/lists');
	}
	
	
	// 添加
	public function add(){
		
		$topmenu = $this->topmenu;
		$sidebarmenu = $this->sidebarmenu[I('get.k')];
		
		$this->assign('topmenu',$topmenu);
		$this->assign('sidebarmenu',$sidebarmenu);
		$this->display('Role/add');
	}
	
	// 添加操作
	public function do_add(){
		if (!IS_AJAX) {
			$this->error('非法操作');
		}
		
		$name = I('name','','strip_tags,htmlspecialchars,trim');
		if (empty($name)) {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'角色名称不能为空','field'=>''));
		}
		$roleModel = M('Role');
		
		// 角色名称已经存在
		if ($roleModel->where(array('name'=>$name,'status'=>array('NEQ',-1)))->count()){
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'该角色名称已存在，请换一个再试','field'=>''));
		}
		
		$status = I('status',0,'intval');
		
		$data['name'] = $name;
		$data['createtime'] = NOW_TIME;
		$data['status'] = $status;

		if ($roleModel->add($data)) {
			$this->ajaxReturn(array('error'=>0,'errmsg'=>'操作成功','field'=>''));
		} else {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'操作失败','field'=>''));
		}
	}
	
	// 编辑
	public function edit(){
		$id = I('id',0,'intval');
		if (!$id) {
			$this->error('参数错误');
		}
		$info = M('Role')->where(array('id'=>$id))->find();
		if (empty($info)) {
			$this->error('没有找到相应记录');
		}
		
		$topmenu = $this->topmenu;
		$sidebarmenu = $this->sidebarmenu[I('get.k')];
		
		$this->assign('topmenu',$topmenu);
		$this->assign('sidebarmenu',$sidebarmenu);
		$this->assign('info',$info);
		$this->display('Role/edit');
	}
	
	// 编辑操作
	public function do_edit(){
		if (!IS_AJAX) {
			$this->error('非法操作');
		}
		$id = I('id',0,'intval');
		if (!$id) {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'参数错误','field'=>''));
		}
		
		$name = I('name','','strip_tags,htmlspecialchars,trim');
		if (empty($name)) {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'角色名称不能为空','field'=>''));
		}
		
		$roleModel = M('Role');
		
		// 角色名称已经存在
		if ($roleModel->where(array('name'=>$name,'id'=>array('NEQ',$id),'status'=>array('NEQ',-1)))->count()){
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'该角色名称已存在，请换一个再试','field'=>''));
		}
		
		$status = I('status',0,'intval');
		
		$data['name'] = $name;
		$data['status'] = $status;
		
		if ($roleModel->where(array('id'=>$id))->save($data) !== false) {
			$this->ajaxReturn(array('error'=>0,'errmsg'=>'操作成功','field'=>''));
		} else {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'操作失败','field'=>''));
		}
	}
	
	// 删除操作
	public function del(){
		if (!IS_AJAX) {
			$this->error('非法操作');
		}
		$id = I('id',0,'intval');
		if (!$id) {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'参数错误','field'=>''));
		}
		if (M('Role')->where(array('id'=>$id))->save(array('status'=>-1)) !== false) {
			$this->ajaxReturn(array('error'=>0,'errmsg'=>'操作成功','field'=>''));
		} else {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'操作失败','field'=>''));
		}
	}
	
	// 角色授权
	public function right(){
		$id = I('id',0,'intval');
		if (!$id) {
			$this->error('参数错误');
		}
		
		$roleModel = M('Role');
		$role = $roleModel->where(array('status'=>array('NEQ',-1),'id'=>$id))->find();

		if (empty($role)) {
			if (IS_AJAX) {
				$this->ajaxReturn(array('error'=>1,'errmsg'=>'没有找到相应记录','field'=>''));
			} else {
				$this->error('没有找到相应记录');
			}
		}
		
		if (IS_AJAX) {
			$privilege = I('privilege','');
			if (!empty($privilege) && is_array($privilege)) {
				$privilege = implode($privilege, ',');
			} else {
				$privilege = '';
			}
			if ($roleModel->where(array('id'=>$role['id']))->save(array('right'=>$privilege)) !== false) {
				$this->ajaxReturn(array('error'=>0,'errmsg'=>'操作成功','field'=>''));
			} else {
				$this->ajaxReturn(array('error'=>1,'errmsg'=>'操作失败','field'=>''));
			}
		}
		
		$right = getPermission();
		
		$_roleprivi = !empty($role['right'])?explode(',', $role['right']):array();
		
		$topmenu = $this->topmenu;
		$sidebarmenu = $this->sidebarmenu[I('get.k')];
		
		$this->assign('topmenu',$topmenu);
		$this->assign('sidebarmenu',$sidebarmenu);
		$this->assign('role',$role);
		$this->assign('_roleprivi',$_roleprivi);
		$this->assign('right',$right);
		$this->display('Role/right');
	}
	
}